<template>
    <full-screen-dialog
        :title="titleMap[mode]"
        v-model="visible"
        :width="1200"
        destroy-on-close
        @closed="$emit('closed')"
    >
        <el-form
            :model="entityObj"
            :rules="rules"
            :disabled="mode == 'Show'"
            ref="dialogForm"
            label-width="100px"
        >
            <el-row>
                <!-- <el-col :span="6">
                    <el-form-item label-width="80px" label="还货单位:">
                        <el-tooltip placement="top">
                            <template #content>
                                联系人: {{entityObj.purchaseRealName}}
                                <br />
                                联系电话:{{entityObj.purchaseMobile}}
                                <br />
                                联系地址:{{entityObj.purchaseAddress}}
                            </template>
                            <span style="color: blue;">{{entityObj.purchaseUnityName}}</span>
                        </el-tooltip>
                    </el-form-item>
                </el-col>-->
                <el-col :span="6">
                    <el-form-item label-width="80px" label="借货人" prop="borrowGoodsOrderName">
                        <el-input
                            placeholder="---请输入还货人---"
                            v-model="entityObj.borrowGoodsOrderName"
                            style="width: 100%"
                        />
                    </el-form-item>
                </el-col>
                <el-col :span="6">
                    <el-form-item label-width="80px" label="借货时间" prop="borrowGoodsOrderDate">
                        <el-date-picker
                            v-model="entityObj.borrowGoodsOrderDate"
                            :default-time="defaultTime"
                            type="datetime"
                            value-format="YYYY-MM-DD"
                            format="YYYY-MM-DD"
                            placeholder="请输入采购时间"
                            style="width: 100%"
                        />
                    </el-form-item>
                </el-col>
                <el-col :span="6">
                    <el-form-item label-width="80px" label="借货单位" prop="taskOrgId">
                        <sc-supplier-select
                            ref="taskSupplierSelect"
                            :queryType="'supplier/pages'"
                            :props="supplierProps"
                            :selectKeyValue="'supplierId'"
                            :multiple="false"
                            v-model="entityObj.supplier"
                            @fatherMethod="supplierSelectProcess"
                        ></sc-supplier-select>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="12">
                    <el-form-item label-width="80px" label="借货订单" prop="purchaseOrderItems">
                        <el-button size="small" @click="addBorrowMaterial()">借货</el-button>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="24">
                    <el-form-item label-width="80px">
                        <el-table
                            :data="entityObj.borrowGoodsOrderItems"
                            border
                            style="width: 100%"
                            show-summary
                        >
                            <el-table-column label="操作">
                                <template #default="scope">
                                    <div
                                        @click="deleteBorrowRow(scope.$index, scope.row)"
                                        class="del-temp-but"
                                    >移除</div>
                                </template>
                            </el-table-column>
                            <el-table-column label="物料编码" width="130" prop="materialCode"></el-table-column>
                            <el-table-column label="物料名称" width="130" prop="materialName">
                                <template #default="scope">
                                    <div
                                        v-if="scope.row.materialImages && scope.row.materialImages.length > 0"
                                        style="color: #318cf2;"
                                        @click="handlePreview(scope.row)"
                                    >{{scope.row.materialName}}</div>
                                    <div v-else>{{scope.row.materialName}}</div>
                                </template>
                            </el-table-column>
                            <el-table-column label="规格" width="130" prop="materialSpecs"></el-table-column>
                            <el-table-column label="物料类别" width="130" prop="materialTypeName"></el-table-column>
                            <el-table-column label="单位" width="100" prop="unitName"></el-table-column>
                            <el-table-column prop="borrowQty" label="借货数量" min-width="130">
                                <template #default="scope">
                                    <el-input-number
                                        v-model="scope.row.borrowQty"
                                        style="width: 100%"
                                        :step="1"
                                        controls-position="right"
                                    ></el-input-number>
                                </template>
                            </el-table-column>
                            <el-table-column label="备注" min-width="160" prop="remarks">
                                <template #default="scope">
                                    <el-input v-model="scope.row.remarks" style="width: 100%" />
                                </template>
                            </el-table-column>
                        </el-table>
                    </el-form-item>
                </el-col>
            </el-row>
        </el-form>
        <template #footer>
            <el-button @click="handleBeforeClose">取 消</el-button>
            <el-button v-if="mode != 'show'" type="primary" :loading="isSaveing" @click="submit(0)">暂 存</el-button>
            <el-button v-if="mode != 'show'" type="primary" :loading="isSaveing" @click="submit(1)">保 存</el-button>
        </template>
    </full-screen-dialog>
    <!-- 预览组件 -->
    <el-image-viewer
        v-if="showImage"
        @close="closeViewer"
        :hide-on-click-modal="true"
        :url-list="srcList"
    />

    <!-- 借出订单记录 -->
    <template>
        <el-dialog
            v-model="materialState"
            title="选择商品"
            :width="1300"
            :center="true"
            append-to-body
            style="height: 480px"
        >
            <el-row style="margin-bottom: 40px;">
                <sc-material-choice ref="scMaterialChoice" :queryType="'material/item-pages'"></sc-material-choice>
            </el-row>
            <template #footer>
                <el-button @click="materialState = false">取 消</el-button>
                <el-button type="primary" :loading="isSaveing" @click="setMaterialChoice">确定</el-button>
            </template>
        </el-dialog>
    </template>
</template>



<script>
import tool from "@/utils/tool";
export default {
    emits: ["success", "closed"],
    data() {
        return {
            visible: false,
            active: 2,
            stepList: ["已下单", "处理中", "已完成"],
            orgType: 3,
            mode: "Add",
            titleMap: {
                Add: "新增",
                Edit: "编辑",
                Show: "查看",
            },
            entityObj: {},
            //预览图片
            previewUrl: [],
            //预览图片列表
            srcList: [],
            showImage: false,
            supplierProps: {
                label: "supplierName",
                value: "supplierId",
            },
            //还货
            materialState: false,
            borrowQueryList: {},
            taskSubSupplierOptions: [],
        };
    },
    computed: {},
    methods: {
        //显示=
        open(mode = "Add") {
            this.mode = mode;
            this.visible = true;
            return this;
        },
        //图片预览
        handlePreview(row) {
            console.log("row", row.materialImages);
            if (!row.materialImages || row.materialImages.length == 0) {
                return;
            }
            this.previewUrl = row.materialImages[0]; // 设置当前预览的图片 URL
            this.srcList = row.materialImages; // 设置预览列表
            this.showImage = true; // 显示 el-image
        },
        closeViewer() {
            this.showImage = false;
        },
        //表单注入数据
        async setData(data) {
            if (data && data.borrowGoodsOrderId) {
                var returnValue = await this.$API.auth.GetProcessApi.get(
                    `borrow-goods-order/${data.borrowGoodsOrderId}`,
                    {}
                );
                if (returnValue.status == 200) {
                    this.entityObj = returnValue.response;
                    this.entityObj.supplier={supplierId:this.entityObj.supplierId,supplierName:this.entityObj.supplierName}
                }
            }
        },
        submit(status) {
            this.$refs.dialogForm.validate(async (valid) => {
                if (valid) {
                    this.isSaveing = true;
                    try {
                        let data = structuredClone(this.entityObj);
                        var returnValue =
                            await this.$API.auth.PostProcessApi.post(
                                this.mode == "Add"
                                    ? "borrow-goods-order/create"
                                    : "borrow-goods-order/update",
                                { ...data,status:status }
                            );
                        if (returnValue.status == 200) {
                            this.$message.success("操作成功");
                            this.$emit("fatherMethod");
                            this.isSaveing = false;
                            this.visible = false;
                        } else {
                            this.$message.error("操作失败:" + returnValue.msg);
                            this.isSaveing = false;
                        }
                    } catch (error) {
                        this.loading = false;
                        this.isSaveing = false;
                        return false;
                    }
                }
            });
        },
        //收货供应商
        async supplierSelectProcess(rowData) {
            this.entityObj.supplierId = rowData.supplierId;
            this.entityObj.supplierName = rowData.supplierName;
        },
        //还货
        addBorrowMaterial() {
            this.materialState = true;
            this.$nextTick(() => {
                this.$refs.scMaterialChoice.opBindData();
            });
        },
        //还货
        setMaterialChoice() {
            if (
                !this.entityObj.borrowGoodsOrderItems ||
                this.entityObj.borrowGoodsOrderItems.length == 0
            ) {
                this.entityObj.borrowGoodsOrderItems = [];
            }

            let materials = this.$refs.scMaterialChoice.selectionRows;
            materials.map((p) => {
                this.entityObj.borrowGoodsOrderItems.push({
                    materialId: p.materialId,
                    materialItemId: p.materialItemId,
                    materialCode: p.materialCode,
                    materialName: p.materialName,
                    materialSpecs: p.materialSpecs,
                    materialTypeId: p.materialTypeId,
                    materialTypeName: p.materialTypeName,
                    unitCode: p.unitCode,
                    unitName: p.unitName,
                    borrowQty: 1,
                    remarks: "",
                });
            });
            this.materialState = false;
        },
        //删除
        deleteBorrowRow(index) {
            this.entityObj.borrowGoodsOrderItems.splice(index, 1);
        },
        async initOptions() {},
    },
    async mounted() {
        this.orgType = tool.data.get("USER_INFO").AccountType;
        this.entityObj.borrowGoodsOrderName = tool.data.get("USER_INFO").Name;
        this.entityObj.borrowGoodsOrderDate = tool.formatDate(new Date(), 1);
    },
};
</script>

<style scoped>
.el-form-item {
    margin-bottom: 5px !important;
}

.del-temp-but {
    text-align: center;
    width: 46px;
    height: 24px;
    background: #ffffff;
    border-radius: 4px;
    border: 1px solid #318cf2;
    font-family: Microsoft YaHei;
    font-weight: 400;
    font-size: 14px;
    color: #318cf2;
}
</style>
